package cn.itcast.db_util;

import java.sql.*;

/**
 * Created 吴海杰 on 2017/2/28.
 * jdbc工具类
 */

public class JdbcUtil {
    private static String url = "jdbc:mysql://localhost:3306/day0224";
    private static String user = "whj";
    private static String password = "whj";
/*静态代码块（只加载一次）
* */
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("驱动程序注册出错");
        }
    }


    public static Connection getConnection() {
try {


        Connection con = DriverManager.getConnection(url, user, password);
        return con;
    }catch (SQLException e){
    e.printStackTrace();
    throw new RuntimeException(e);
    }
    }

    public static void close(Connection con, Statement pstmt,ResultSet rs){
        if(rs!=null){
            try {
                rs.close();
                rs=null;
            }catch (SQLException e){
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
        if(pstmt!=null){
            try {
                pstmt.close();
            }catch (SQLException e){
                e.printStackTrace();
               throw new RuntimeException(e);
            }
        }
        if (con!=null ){
           try {
               con.close();
           }catch (SQLException e){
               e.printStackTrace();
               throw new RuntimeException(e);
           }
        }
    }

}